<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);
class Announcement extends Super_admin {

    function __construct() {
        parent::__construct();

        $this->load->model(array(
            'User_model',
            'Announcement_model'
        ));

        $this->load->helper(array(
            'security',
            'string',
            'download',
        ));
    }

    function index() {
        $this->list_records();
    }

    /**
     * list_records
     *
     * Load list
     *
     * @access public
     * @param void
     * @return void
     */
    function list_records() {
        $this->load->view('announcement/list');
    }
    
    function get_announcements() {
        
        $where = array();
        $select = 'a.announcement_id, a.title, CONCAT(u.lname, ", ",u.fname) author, a.date_posted, 0 ops';

        $page = $this->input->post('page');
        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        if ($this->input->post('sidx') == 'category')
            $order_by .= ', sub_category';

        $total = $this->Announcement_model->get_total($where);
        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        $join_user = array('table' => USERS . ' u'
            , 'condition' => 'u.user_id = a.user_id'
        );

        $args = array(
                    'select' => $select,
                    'where' =>  array(),
                    'join' => array($join_user),
                    'limit' => LIMIT,
                    'offset' => $offset,
                    'order_by' => $order_by,
                    'resource_id' => TRUE,
        );

        $records = $this->Announcement_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'announcement_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }
    
    function add(){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $data = array(
            'announcement_id' => 0,
            'title' => '',
            'description' => '',
        );
        
        $this->load->view('announcement/form', $data);
        
    }
    
    function edit($id){
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller

        $join_user = array('table' => USERS . ' u'
            , 'condition' => 'u.user_id = a.user_id'
        );

        $params = array(
                'select' => '*',
                'where' => array('announcement_id' => $id),
                'join' => array($join_user),
                'order_by' => NULL
        );

        $data = $this->Announcement_model->get_record($params);

        $this->load->view('announcement/form', $data);
        
        
    }
    
    function view($id){
        $join_user = array('table' => USERS . ' u'
            , 'condition' => 'u.user_id = a.user_id'
        );
        
        $params = array(
                'select' => '*, CONCAT(u.lname, ", ",u.fname) author',
                'where' => array('announcement_id' => $id),
                'join' => array($join_user),
                'order_by' => NULL
        );

        $data = $this->Announcement_model->get_record($params);

        $this->load->view('announcement/display', $data);
        
        
    }
    
    function submit(){
        
        $response = FALSE;
        $announcement_id = $this->input->post('announcement_id', TRUE);

        $data = array(
            'title' => $this->input->post('title',TRUE),
            'description' => $this->input->post('description',TRUE),
            'date_posted' => pdate(),
            'user_id' => _session('user_id')
        );


        if (empty($announcement_id)) {

            $data['file_name'] = $this->upload_file($announcement_id = 0);

            $this->Announcement_model->_insert($data);

            $msg = sprintf(lang('cms_add_success'), "Announcement");
            $response = TRUE;

        } else {
            $data['file_name'] = $this->upload_file($announcement_id,'U');

            if (empty($data['file_name'])) {
                unset($data['file_name']);
            }

            $where = array('announcement_id' => $announcement_id);

            $this->Announcement_model->_update($where, $data);

            $msg = sprintf(lang('cms_edit_success'), "Announcement");


            $response = TRUE;
        }

        return redirect('announcement/list_records/');
    }


    function upload_file($annoucement_id = 0, $mode = 'A')
    {
        $upload_config['upload_path'] = ANNOUNCEMENT_FOLDER;
        $upload_config['allowed_types'] = '*';
        $upload_config['encrypt_name'] = FALSE;
        $upload_config['remove_spaces'] = TRUE;
        $upload_config['overwrite'] = FALSE;
        $upload_config['max_size'] = '2000';
        $image_data = NULL;

        $this->load->library('upload');
        $this->upload->initialize($upload_config);

        if ( ! empty($_FILES['file_name']['name']) ) {
            if ( ! $this->upload->do_upload('file_name'))
            {
                _error(sprintf(lang('cms_custom'), join('<br />', $this->upload->error_msg)), FALSE);
                return redirect('announcement/list_records/');
            }

            $file_data = $this->upload->data();
        }
        
        //If in edit mode
        if ($mode == 'U') {
            $where = 'announcement_id =' . $annoucement_id;

            $file_name = $this->Announcement_model->get_value('file_name', $where);

            if ( ! empty($_FILES['file_name']['name']) ) {
                if ($file_data['file_name'] != $file_name && $file_name != '') {
                    unlink(ANNOUNCEMENT_FOLDER.$file_name);
                }
            }
        }

        //_success(sprintf(lang('cms_edit_success'), $this->c), FALSE);
        if (empty($file_data)) {
            return '';
        }
        else {
            return $file_data['file_name'];
        }
    }

    function download_file($id) {

        $where = 'announcement_id =' . $id;
        $file_name = $this->Announcement_model->get_value('file_name', $where);

        $data = file_get_contents(ANNOUNCEMENT_FOLDER.$file_name); // Read the file's contents

        return force_download($file_name, $data);

    }
    
    function delete($annoucement_id) {
        $access = array('admin');
        $this->_is_authorize($access); //check super_admin controller
        
        _success(sprintf(lang('cms_delete_success'), "Annoucement"));

        $data = $this->Announcement_model->get_record(array(
            'select' => 'file_name',
            'where' => array('announcement_id' => $annoucement_id)
        ));

        if (! empty($data['file_name'])) unlink(ANNOUNCEMENT_FOLDER.$data['file_name']);

        $this->Announcement_model->_delete(array('announcement_id' => $annoucement_id));

        $msg = sprintf(lang('cms_delete_success'), "Announcement");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
    }

  

}
/* End of file annoucement.php */
/* Location: ./application/controllers/annoucement.php */
